//
//  DEWSafeTableViewCell.m
//  OtherDemo
//
//  Created by lianjie on 2018/7/25.
//  Copyright © 2018年 lianjie. All rights reserved.
//

#import "DEWSafeTableViewCell.h"
#import "MMButton.h"
@implementation DEWSafeTableViewCell


-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.backgroundColor = ClearColor;
        self.contentView.backgroundColor = ClearColor;
    }
    return self;
}

- (void)setModel:(DEWSafeModel *)model
{
    _model = model;
    
    ///背景
    UIView *bgView = [[UIView alloc]init];
    bgView.backgroundColor = [UIColor whiteColor];
    bgView.frame = CGRectMake(Margin_15, Margin_15, Device_Width-Margin_15*2, model.cellHeight);
    bgView.layer.cornerRadius = 4.0f;
    bgView.layer.masksToBounds = YES;
    [self.contentView addSubview:bgView];
    
    
    ///标题
    UILabel *titleLab = [[UILabel alloc]init];
    titleLab.frame = CGRectMake(Margin_15, Margin_15, bgView.width-Margin_15*2, 23);
    [bgView addSubview:titleLab];
    
    NSString *signStr = @"• ";
    
    if ([model.isRead isEqualToString:@"0"]) {
        ///未读
        signStr = @"• ";
    }else{
        ///已读
        signStr = @"";
    }
    NSString *titleStr = [NSString stringWithFormat:@"%@%@",signStr,model.title];
    
    NSMutableAttributedString *attributeTitle = [[NSMutableAttributedString alloc]initWithString:titleStr];
    [attributeTitle setAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16],NSForegroundColorAttributeName:Color_33} range:NSMakeRange(signStr.length, model.title.length)];
    if ([model.isRead isEqualToString:@"0"]) {
        [attributeTitle setAttributes:@{NSForegroundColorAttributeName:UIColorFromRGB(0xFF492C),NSFontAttributeName:[UIFont systemFontOfSize:14]} range:NSMakeRange(0, signStr.length)];
    }
    titleLab.attributedText = attributeTitle;
    
    ///内容
    UILabel *descLab = [[UILabel alloc]initWithFrame:CGRectMake(Margin_15, titleLab.Wbotoom+13, bgView.width-Margin_15*2, model.cellHeight-90)];
    descLab.numberOfLines = 0;
    descLab.attributedText = model.contentText;
    [bgView addSubview:descLab];
    
    ///时间图片
    UIImageView *timeImageV = [[UIImageView alloc]init];
    timeImageV.left = Margin_15;
    timeImageV.Wbotoom = bgView.height-Margin_15-10;
    timeImageV.size = CGSizeMake(10, 10);
    timeImageV.image = [UIImage imageNamed:@"clock_message"];
    timeImageV.backgroundColor = CellLineColor;
    [bgView addSubview:timeImageV];
    
    ///时间和来源
    UILabel *timeSourceLab = [[UILabel alloc]init];
    timeSourceLab.left = timeImageV.right+8;
    timeSourceLab.Wbotoom = bgView.height-27;
    timeSourceLab.size = CGSizeMake(bgView.width-timeImageV.right-8-80, 15);
    timeSourceLab.font = [UIFont systemFontOfSize:11];
    timeSourceLab.textColor = UIColorFromRGB(0xAAAAAA);
    [bgView addSubview:timeSourceLab];
    
    
    NSString *sourceType = @"";
    if ([model.source isEqualToString:@"1"]) {
        sourceType = @"账号安全中心";
    }else{
        sourceType = @"邻里公社团队";
    }
    
    NSString *timeStr = [DateHelper getTimeWithTimeIntervalString:model.time];
    NSString *bottomStr = [NSString stringWithFormat:@"%@    %@",timeStr,sourceType];
    timeSourceLab.text = bottomStr;
    
    ///底部按钮
    
    NSString *btnTitle = @"未读";
    MMButton *btn = [MMButton buttonWithType:UIButtonTypeCustom];
    int tagg = [model.type intValue];
    
    if (tagg == 1) {
        btnTitle = @"添加密码";
    }else if (tagg == 5){
        
        btnTitle = @"完善资料";
    }else if (tagg == 8){
        
        btnTitle = @"";
    }
    if ([model.isRead isEqualToString:@"1"]) {
        btnTitle = @"已读";
        btn.userInteractionEnabled = NO;
    }
    
    
    btn.titleLabel.font = [UIFont systemFontOfSize:11];
    [btn setTitleColor:Color_99 forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(safeBtnAction) forControlEvents:UIControlEventTouchUpInside];
    btn.imageAlignment = MMImageAlignmentRight;
    btn.spaceBetweenTitleAndImage = 4.0f;
    
    btn.right = bgView.width-Margin_15;
    btn.Wbotoom = bgView.height-12;
    btn.size = CGSizeMake(60, 15);
    [bgView addSubview:btn];
    
    
    
    if ([model.type isEqualToString:@"8"]) {
        ///不同意
        
        ///底部按钮
        UIView *line = [[UIView alloc]init];
        line.backgroundColor = UIColorFromRGB(0xEFEFEF);
        line.left = 0;
        line.Wbotoom = bgView.height-46.0f;
        line.size = CGSizeMake(bgView.width, 1.0f);
        [bgView addSubview:line];
        
        UIView *bottomView = [[UIView alloc]init];
        bottomView.x = 0;
        bottomView.Wbotoom = 5;
        bottomView.width = bgView.height;
        bottomView.height = 40.0f;
        bottomView.backgroundColor = UIColorFromRGB(0xEFEFEF);
        [bgView addSubview:bottomView];
        
        UIButton *btn01 = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn01 setTitle:@"允许" forState:UIControlStateNormal];
        btn01.titleLabel.font = [UIFont systemFontOfSize:15];
        btn01.tag = 101;
        [btn01 setTitleColor:Color_99 forState:UIControlStateNormal];
        [btn01 addTarget:self action:@selector(bottomViewAction:) forControlEvents:UIControlEventTouchUpInside];
        btn01.frame = CGRectMake(0, 0, bottomView.width/2-1, bottomView.height);
        [bottomView addSubview:btn01];
        
        
        UIButton *btn02 = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn02 setTitle:@"不允许" forState:UIControlStateNormal];
        btn02.titleLabel.font = [UIFont systemFontOfSize:15];
        btn02.tag = 102;
        [btn02 setTitleColor:Color_99 forState:UIControlStateNormal];
        [btn02 addTarget:self action:@selector(bottomViewAction:) forControlEvents:UIControlEventTouchUpInside];
        btn02.frame = CGRectMake(0, 0, bottomView.width/2-1, bottomView.height);
        [bottomView addSubview:btn02];
    }
}

- (void)safeBtnAction
{
    if (_safeDelegate &&[_safeDelegate respondsToSelector:@selector(safeCellAtItemIndex:andModel:)]) {
        
        [_safeDelegate safeCellAtItemIndex:self.indexPath andModel:self.model];
        
    }
    
}

- (void)bottomViewAction:(UIButton *)sender
{
//    NSInteger tagg = sender.tag;
    
    
    
}


- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
